<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css" />
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field="name">名称</th>
							<th class="text-center" data-field="tel1">联系电话</th>
							<th class="text-center" data-field="person">负责人</th>
							<th class="text-center" data-field="s1">地址</th>
							<th class="text-center" data-field="type">所属类型</th>
							<th class="text-center" data-field="createTime">创建时间</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">编辑法律服务</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="name" class="control-label">名称：</label> <input type="text" class="form-control" id="name">
					</div>
					<div class="form-group">
						<label for="s1" class="control-label">地址(请输入详细地址，以便于app上导航)：</label> <input type="text" class="form-control" id="s1">
					</div>
					<div class="form-group">
						<label for="tel1" class="control-label">联系电话：</label> <input type="text" class="form-control" id="tel1">
					</div>
					<div class="form-group">
						<label for="person" class="control-label">负责人：</label> <input type="text" class="form-control" id="person">
					</div>
					<div class="form-group">
						<label for="licence" class="control-label">执行许可证：</label> <input type="text" class="form-control" id="licence">
					</div>
					<div class="form-group">
						<label for="approvalTime" class="control-label">批准日期：</label> <input type="text" class="form-control" id="approvalTime">
					</div>
					<div class="form-group">
						<label for="fax" class="control-label">传真号码：</label> <input type="text" class="form-control" id="fax">
					</div>
					<div class="form-group">
						<label for="tel2" class="control-label">负责人电话：</label> <input type="text" class="form-control" id="tel2">
					</div>
					<div class="form-group">
						<label for="type" class="control-label">所属类型：</label> <select id="type" class="form-control">
							<option value="">请选择...</option>
						</select>
					</div>
				</div>
				<div class="modal-footer">
					<button id="submitBtn" type="button" class="btn btn-primary" onclick="insertLegalService();">提交</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script src="${pageContext.request.contextPath}/plugins/laydate/laydate.js"></script>
<script>
	var selectId = '';
	laydate.render({
		elem : '#approvalTime'
	});
	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-success btn-xs glyphicon glyphicon-file' onclick='showModal(\"" + row.id + "\");'>查看详情</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delLegalService(\"" + row.id + "\")'>删除信息</button>", ].join('');
	}

	var table_dataset = [ {
		field : 'name',
		align : 'center',
	}, {
		field : 'tel1',
		align : 'center',
	}, {
		field : 'person',
		align : 'center',
	}, {
		field : 's1',
		align : 'center',
	}, {
		field : 'typeStr',
		align : 'center',
	}, {
		field : 'createTime',
		align : 'center',
		formatter : function timeFormatter(value, row, index) {
			return (new Date(row.createTime)).Format("yyyy-MM-dd hh:mm:ss");
		}
	}, {
		align : 'center',
		width : 300,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/civilian/seleteLegalServicePage",
		idField : "sid",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ],
	});

	$(function() {
		$.ajax({
			url : "${pageContext.request.contextPath}/civilian/seleteCivilianTypeByCode",
			type : "post",
			data : {
				code : "flfw"
			},
			dataType : "json",
			success : function(data) {
				var list = data['list'];
				var types = "<option value=''>请选择...</option>";
				for (var i = 0; i < list.length; i++) {
					types += "<option value='"+list[i].code+"'>" + list[i].name + "</option>";
				}
				$("#type").html(types);
			}
		});
	});

	function showModal(id) {
		selectId = id;
		if (id == '') {
			$("#name").val('');
			$("#s1").val('');
			$("#tel1").val('');
			$("#tel2").val('');
			$("#person").val('');
			$("#licence").val('');
			$("#approvalTime").val('');
			$("#fax").val('');
			$("#type").val('');
		} else {
			$.ajax({
				url : "${pageContext.request.contextPath}/civilian/seleteLegalServiceById",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(map) {
					var legalService = map['legalService'];
					$("#name").val(legalService.name);
					$("#s1").val(legalService.s1);
					$("#tel1").val(legalService.tel1);
					$("#tel2").val(legalService.tel2);
					$("#person").val(legalService.person);
					$("#licence").val(legalService.licence);
					$("#approvalTime").val((new Date(legalService.approvalTime)).Format("yyyy-MM-dd"));
					$("#fax").val(legalService.fax);
					$("#type").val(legalService.type);
				}
			});
		}
		$('#modal').modal({
			backdrop : 'static',
			keyboard : false
		});
	}

	function insertLegalService() {
		$("#submitBtn").button('loading');
		var name = $("#name").val();
		var s1 = $("#s1").val();
		var tel1 = $("#tel1").val();
		var tel2 = $("#tel2").val();
		var person = $("#person").val();
		var licence = $("#licence").val();
		var approvalTime = $("#approvalTime").val();
		var fax = $("#fax").val();
		var type = $("#type").val();
		if (name === '' || s1 === '' || tel1 === '' || tel2 === '' || person === '' || type === '') {
			layer.alert("新增的数据中填写不完整!");
			$("#submitBtn").button('reset');
			return;
		}
		if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(tel1))) {
			layer.alert("联系电话格式不正确！");
			$("#submitBtn").button('reset');
			return false;
		}
		if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(tel2))) {
			layer.alert("负责人电话格式不正确！");
			$("#submitBtn").button('reset');
			return false;
		}
		$.ajax({
			url : "${pageContext.request.contextPath}/civilian/insertAndUpdateLegalService",
			type : "post",
			data : {
				id : selectId,
				name : name,
				s1 : s1,
				tel1 : tel1,
				tel2 : tel2,
				person : person,
				licence : licence,
				approvalTimeStr : approvalTime,
				fax : fax,
				type : type
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result == 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
				$("#submitBtn").button('reset');
			}
		});
	}

	function delLegalService(id) {
		layer.confirm("是否删除此项？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/civilian/delLegalService",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}
</script>
</html>
